server {

    listen 88;
    server_name www.technologycloud.net;
    
    default_type 'text/html';
    charset utf-8,gbk;

    lua_code_cache off;


    location /lua {
        # lua_code_cache off;
        access_by_lua_file /opt/andrew_git/openresty_controller/lua/forbidden_list.lua;
        default_type 'text/html';
        content_by_lua 'ngx.say("hello world22")';
    }

    

    location /redis_test/set {
        default_type 'text/html';
        content_by_lua_block {
            -- http://www.technologycloud.net:88/redis_test/set?redis_key=msg&redis_value=12
            local redis = require "resty.redis"
            local red = redis.new()
            red:set_timeout(1000)
            red:connect('127.0.0.1', '6379')
            local res, err = red:auth('123456')

            if not res  then
                ngx.say("reddis连接失败:",res,'<br />')
                ngx.say("reddis连接失败:",err,'<br />')
                return
            end



            local redis_key = nil
            local redis_value = nil
            if ngx.req.get_method() == "GET" then
                local args_query = ngx.req.get_uri_args()
                redis_key = args_query['redis_key']
                redis_value = args_query['redis_value']
            end 

            if not redis_key then
                ngx.say("redis_key参数缺失!")
                return
            end

            if not redis_value then
                ngx.say("redis_value参数缺失!")
                return
            end


            
            local ok,err 
            ok, err = red:set(redis_key, redis_value)  

            ngx.say(ok)

            
        }
    }

    location /redis_test {
        default_type 'text/html';
        content_by_lua_block {
            -- http://www.technologycloud.net:88/redis_test?redis_key=msg
            local redis = require "resty.redis"
            local red = redis.new()
            red:set_timeout(1000)
            red:connect('127.0.0.1', '6379')
            local res, err = red:auth('123456')

            if not res  then
                ngx.say("reddis连接失败:",res,'<br />')
                ngx.say("reddis连接失败:",err,'<br />')
                return
            end


            -- local ok,err 
            -- ok, err = red:set("msg", "hello world")  

            local redis_key = nil
            if ngx.req.get_method() == "GET" then
                local args_query = ngx.req.get_uri_args()
                redis_key = args_query['redis_key']
            end 

            if not redis_key then
                ngx.say("redis_key参数缺失!")
                return
            end

            local resp, err = red:get(redis_key)  
            if not resp then  
                ngx.say("get msg error : ", err)  
                --return close_redis(red)  
            end

            ngx.say("结果 : ", resp)

            
        }
    }



    location /token_need_test {
        lua_code_cache off;
        # access_by_lua_file /opt/andrew_git/openresty_controller/lua/token_test.lua;
        default_type 'text/html';
        # content_by_lua 'ngx.say("hello world3")';
        content_by_lua_block {

            local token = nil
            if ngx.req.get_method() == "GET" then
                local args_query = ngx.req.get_uri_args()
                token = args_query['token']
            end 
            ngx.say(token)
        }
    }

    location /mysql {
        lua_code_cache off;
        # access_by_lua_file /opt/andrew_git/openresty_controller/lua/token_test.lua;
        default_type 'text/html';
        # content_by_lua 'ngx.say("hello world3")';
        content_by_lua_block {

            local mysql = require("resty.mysql")
            local db, err = mysql:new()
            db:set_timeout(5000)
            local props = {
                host = "47.103.36.143",
                port = 3307,
                database = "low_code",
                user = "root",
                password = "iop890IOP*()"
              }

            local res, err, errno, sqlstate = db:connect(props)

            db:query('SET NAMES utf8')

            local select_sql = "select * from user"
            res, err, errno, sqlstate = db:query(select_sql)

            local cjson = require("cjson")

            for i, row in ipairs(res) do
                for name, value in pairs(row) do
                    -- ngx.say(name,value)
                end
            end

            ngx.say(cjson.encode({resultCode = 500,resultMessage = res,resultData=res}))

         

            if db then
                db:close()
            end
            ngx.say("<br/>")
           
        }
    }


    location / {
        default_type 'text/html';
        content_by_lua_file /opt/andrew_git/openresty_controller/lua/ip.lua;
    }


    


  


}
